package com.quarters.controller.wx;


import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.quarters.bean.ArticleOrder;
import com.quarters.bean.NoticeOrder;
import com.quarters.bean.UserInfo;
import com.quarters.bean.layuiBean.ResultPage;
import com.quarters.bean.layuiBean.VoPage;
import com.quarters.commen.BaseController;
import com.quarters.util.Respons.ResponseUtil;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 通知订单表 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2019-09-11
 */
@Controller
@RequestMapping("wx/noticeOrder")
public class APPNoticeOrderController extends BaseController {


    @RequestMapping(value = "noticeDetails",method = RequestMethod.GET)
    public String noticeDetails(Model model,Long id ){

            System.out.println("通知id:"+id);
            model.addAttribute("notice",noticeService.getById(id));
            return "wx/noticeDetails";


    }



    /**
     * @return 确认订单
     */
    @RequestMapping(value = "pay.html",method = RequestMethod.GET)
    public String pay(Model model,Long id){
        NoticeOrder noticeOrder = noticeOrderService.getById(id);

        if(noticeOrder.getFlage()==1){
            return "wx/payment";
        }else{
            String result2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(noticeOrder.getAuditTime());
            System.out.println("订单时间审核:"+result2);
            long currentTime = noticeOrder.getAuditTime() + 60*60*1000;
            Date date = new Date(currentTime);
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String nowTime=df.format(date);
            System.out.println("60分钟---nowTime:"+nowTime);
            //获取当前系统时间的日期
            long time = System.currentTimeMillis();
            Date date1 = new Date(time);
            DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String nowTime1=df1.format(date1);
            System.out.println("现在时间---nowTime1:"+nowTime1);
            //前者大于后者返回大于0的数字 反之小于0
            System.out.println("结果:"+nowTime1.compareTo(nowTime));
            if(nowTime.compareTo(nowTime1)<0){
                return "wx/error";
            }else{
                model.addAttribute("articleOrder",noticeOrder);
                return "wx/noticeOrderPay";
            }
        }
    }


    /**
     * @return 完成订单
     */
    @RequestMapping(value = "success_pay.html",method = RequestMethod.GET)
    public String success_pay(Long id,Model model) throws WxErrorException, WxPayException {
        NoticeOrder noticeOrder = noticeOrderService.getById(id);

            //查询支付订单管理员的openid
            //User user =  userService.selectPayOpenId(articleOrder.getId());
            System.out.println("支付:"+noticeOrder.getOpenid());
            model.addAttribute("data",noticeOrderService.successPay(noticeOrder));
            model.addAttribute("moeny",noticeOrder.getOrderMoeny());
            return "wx/success_pay";


    }

    /**
     * @return 咨询订单支付成功回调
     */
    @RequestMapping(value = "/paySuccess")
    @ResponseBody
    public void paySuccess(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter printWriter = response.getWriter();
        try {
            String xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
            WxPayOrderNotifyResult result = xbxWxService.getConsultationWxPayService().parseOrderNotifyResult(xmlResult);
            // 结果正确
            String myOrderId = result.getOutTradeNo();
            String wxOrderId = result.getTransactionId();
            String totalFee = result.getTotalFee()+"";
            System.out.println("回调:"+myOrderId);
            System.out.println("回调:"+wxOrderId);
            System.out.println("回调:"+totalFee);
            //处理订单的业务逻辑，需要判断订单是否已经支付过，否则可能会重复调用
            noticeOrderService.paySuccess(myOrderId, wxOrderId, totalFee);
            // articleOrderService.send(  myOrderId);
            printWriter.print(WxPayNotifyResponse.success("处理成功!"));
        } catch (Exception e) {
            e.printStackTrace();
            printWriter.print(WxPayNotifyResponse.fail(e.getMessage()));
        } finally {
            printWriter.close();
        }
    }


}
